/*
* @Author: Administrator
* @Date:   2017-11-02 15:21:59
* @Last Modified by:   Administrator
* @Last Modified time: 2018-01-05 15:02:58
*/
import { user_display_name, user_nicename, user_email, token, user_id } from './costant';

export function objToStrMap(obj) {
  const strMap = new Map();
  for (let k of Object.keys(obj)) { //eslint-disable-line
    strMap.set(k, obj[k]);
  }
  return strMap;
}
const comm = {
  validate(val, type) {
    // 非空
    if (type === 'require') {
      return !!val;
    }
    // is phone number
    if (type === 'phone') {
      return /^1\d{10}$/.test(val);
    }
    // is email
    if (type === 'email') {
      return /^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/.test(val);
    }
    return false
  },
  // 成功提示信息
  successTip(msg) {
    alert(msg);
  },
  // 失败提示信息
  errorTip(msg) {
    alert(msg);
  },
  setCookie(name, value, expires) {
    const exp = new Date();
    exp.setTime(exp.getTime() + expires);
    document.cookie = `${name}=${escape(value)};expires=${exp.toGMTString()}`;
  },
  getCookie(name) {
    const reg = new RegExp(`(^| )${name}=([^;]*)(;|$)`);
    const arr = document.cookie.match(reg);
    if (arr) { return unescape(arr[2]); }
    return null;
  },
  delCookie(name) {
    const exp = new Date();
    exp.setTime(exp.getTime() - 1);
    const cval = comm.getCookie(name);

    if (cval != null) {
      document.cookie = `${name}=${cval};expires=${exp.toGMTString()}`;
    }
  },
  base64Encode(input) {
    const _keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
    let output = '';
    let chr1
    let chr2
    let chr3
    let enc1
    let enc2
    let enc3
    let enc4
    let i = 0
    input = comm._utf8_encode(input);
    while (i < input.length) {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);
      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;
      if (isNaN(chr2)) { //eslint-disable-line
        enc3 = 64;
        enc4 = 64
      } else if (isNaN(chr3)) { //eslint-disable-line
        enc4 = 64;
      }
      output = output +
        _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
        _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
    }
    return output;
  },
  base64Decode(input) {
    if (!input) { return null; }
    const _keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
    let output = '';
    let chr1
    let chr2
    let chr3
    let enc1
    let enc2
    let enc3
    let enc4
    let i = 0
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '');
    while (i < input.length) {
      enc1 = _keyStr.indexOf(input.charAt(i++));
      enc2 = _keyStr.indexOf(input.charAt(i++));
      enc3 = _keyStr.indexOf(input.charAt(i++));
      enc4 = _keyStr.indexOf(input.charAt(i++));
      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;
      output += String.fromCharCode(chr1);
      if (enc3 !== 64) {
        output += String.fromCharCode(chr2);
      }
      if (enc4 !== 64) {
        output += String.fromCharCode(chr3);
      }
    }
    output = comm._utf8_decode(output);
    return output;
  },
  _utf8_encode(string) {
    string = string.replace(/\r\n/g, '\n');
    let utftext = '';
    for (let n = 0; n < string.length; n++) {
      const c = string.charCodeAt(n);
      if (c < 128) {
        utftext += String.fromCharCode(c);
      } else if ((c > 127) && (c < 2048)) {
        utftext += String.fromCharCode((c >> 6) | 192);
        utftext += String.fromCharCode((c & 63) | 128);
      } else {
        utftext += String.fromCharCode((c >> 12) | 224);
        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
        utftext += String.fromCharCode((c & 63) | 128);
      }
    }
    return utftext;
  },
  _utf8_decode(utftext) {
    let string = '';
    let i = 0;
    let c2;
    let c1; //eslint-disable-line
    let c3;
    let c = c1 = c2 = 0; //eslint-disable-line
    while (i < utftext.length) {
      c = utftext.charCodeAt(i);
      if (c < 128) {
        string += String.fromCharCode(c);
        i++;
      } else if ((c > 191) && (c < 224)) {
        c2 = utftext.charCodeAt(i + 1);
        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
        i += 2;
      } else {
        c2 = utftext.charCodeAt(i + 1);
        c3 = utftext.charCodeAt(i + 2);
        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
        i += 3;
      }
    }
    return string;
  },
  getUserInfo() {
    const temp = [token, user_display_name, user_nicename, user_email, user_id]
    const ob = {};
    for (let i = 0, { length } = temp; i < length; i++) {
      ob[temp[i]] = comm.base64Decode(comm.getCookie(comm.base64Encode(temp[i])));
    }
    return ob
  },
  deleteUserInfo() {
    const temp = [token, user_display_name, user_nicename, user_email, user_id]
    for (let i = 0, { length } = temp; i < length; i++) {
      comm.delCookie(comm.base64Encode(temp[i]));
    }
  },
}
export default comm

